<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: mcastServer.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>mcastServer.c</h1><a href="mcast_server_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* fpont 12/99   */</span>
<a name="l00002"></a>00002 <span class="comment">/* pont.net      */</span>
<a name="l00003"></a>00003 <span class="comment">/* mcastServer.c */</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;unistd.h&gt;</span> <span class="comment">/* close */</span>
<a name="l00012"></a>00012 
<a name="l00013"></a><a class="code" href="mcast_server_8c.html#c42367fe5c999ec6650de83e9d72fe8c">00013</a> <span class="preprocessor">#define SERVER_PORT 1500</span>
<a name="l00014"></a><a class="code" href="mcast_server_8c.html#a24597a54a085c6c2c33b64138f09eff">00014</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_MSG 100</span>
<a name="l00015"></a>00015 <span class="preprocessor"></span>
<a name="l00016"></a><a class="code" href="mcast_server_8c.html#0ddf1224851353fc92bfbff6f499fa97">00016</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
<a name="l00017"></a>00017 
<a name="l00018"></a>00018   <span class="keywordtype">int</span> sd, rc, n, cliLen;
<a name="l00019"></a>00019   <span class="keyword">struct </span>ip_mreq mreq;
<a name="l00020"></a>00020   <span class="keyword">struct </span>sockaddr_in cliAddr, servAddr;
<a name="l00021"></a>00021   <span class="keyword">struct </span>in_addr mcastAddr;
<a name="l00022"></a>00022   <span class="keyword">struct </span>hostent *h;
<a name="l00023"></a>00023   <span class="keywordtype">char</span> msg[<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>];
<a name="l00024"></a>00024 
<a name="l00025"></a>00025   <span class="keywordflow">if</span>(argc!=2) {
<a name="l00026"></a>00026     printf(<span class="stringliteral">"usage : %s &lt;mcast address&gt;\n"</span>,argv[0]);
<a name="l00027"></a>00027     exit(0);
<a name="l00028"></a>00028   }
<a name="l00029"></a>00029 
<a name="l00030"></a>00030   <span class="comment">/* get mcast address to listen to */</span>
<a name="l00031"></a>00031   h=gethostbyname(argv[1]);
<a name="l00032"></a>00032   <span class="keywordflow">if</span>(h==NULL) {
<a name="l00033"></a>00033     printf(<span class="stringliteral">"%s : unknown group '%s'\n"</span>,argv[0],argv[1]);
<a name="l00034"></a>00034     exit(1);
<a name="l00035"></a>00035   }
<a name="l00036"></a>00036   
<a name="l00037"></a>00037   memcpy(&amp;mcastAddr, h-&gt;h_addr_list[0],h-&gt;h_length);
<a name="l00038"></a>00038   
<a name="l00039"></a>00039   <span class="comment">/* check given address is multicast */</span>
<a name="l00040"></a>00040   <span class="keywordflow">if</span>(!IN_MULTICAST(ntohl(mcastAddr.s_addr))) {
<a name="l00041"></a>00041     printf(<span class="stringliteral">"%s : given address '%s' is not multicast\n"</span>,argv[0],
<a name="l00042"></a>00042            inet_ntoa(mcastAddr));
<a name="l00043"></a>00043     exit(1);
<a name="l00044"></a>00044   }
<a name="l00045"></a>00045 
<a name="l00046"></a>00046   <span class="comment">/* create socket */</span>
<a name="l00047"></a>00047   sd = socket(AF_INET,SOCK_DGRAM,0);
<a name="l00048"></a>00048   <span class="keywordflow">if</span>(sd&lt;0) {
<a name="l00049"></a>00049     printf(<span class="stringliteral">"%s : cannot create socket\n"</span>,argv[0]);
<a name="l00050"></a>00050     exit(1);
<a name="l00051"></a>00051   }
<a name="l00052"></a>00052 
<a name="l00053"></a>00053   <span class="comment">/* bind port */</span>
<a name="l00054"></a>00054   servAddr.sin_family=AF_INET;
<a name="l00055"></a>00055   servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
<a name="l00056"></a>00056   servAddr.sin_port=htons(<a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);  
<a name="l00057"></a>00057   <span class="keywordflow">if</span>(bind(sd,(<span class="keyword">struct</span> sockaddr *) &amp;servAddr, <span class="keyword">sizeof</span>(servAddr))&lt;0) {
<a name="l00058"></a>00058     printf(<span class="stringliteral">"%s : cannot bind port %d \n"</span>,argv[0],<a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);
<a name="l00059"></a>00059     exit(1);
<a name="l00060"></a>00060   }
<a name="l00061"></a>00061 
<a name="l00062"></a>00062   <span class="comment">/* join multicast group */</span>
<a name="l00063"></a>00063   mreq.imr_multiaddr.s_addr=mcastAddr.s_addr;
<a name="l00064"></a>00064   mreq.imr_interface.s_addr=htonl(INADDR_ANY);
<a name="l00065"></a>00065   
<a name="l00066"></a>00066   rc = setsockopt(sd,IPPROTO_IP,IP_ADD_MEMBERSHIP,
<a name="l00067"></a>00067                   (<span class="keywordtype">void</span> *) &amp;mreq, <span class="keyword">sizeof</span>(mreq));
<a name="l00068"></a>00068   <span class="keywordflow">if</span>(rc&lt;0) {
<a name="l00069"></a>00069     printf(<span class="stringliteral">"%s : cannot join multicast group '%s'"</span>,argv[0],
<a name="l00070"></a>00070            inet_ntoa(mcastAddr));
<a name="l00071"></a>00071     exit(1);
<a name="l00072"></a>00072   }
<a name="l00073"></a>00073   <span class="keywordflow">else</span> {
<a name="l00074"></a>00074     printf(<span class="stringliteral">"%s : listening to mgroup %s:%d\n"</span>,
<a name="l00075"></a>00075            argv[0],inet_ntoa(mcastAddr), <a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);
<a name="l00076"></a>00076   
<a name="l00077"></a>00077 
<a name="l00078"></a>00078     <span class="comment">/* infinite server loop */</span>
<a name="l00079"></a>00079     <span class="keywordflow">while</span>(1) {
<a name="l00080"></a>00080       cliLen=<span class="keyword">sizeof</span>(cliAddr);
<a name="l00081"></a>00081       n = recvfrom(sd,msg,<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>,0,(<span class="keyword">struct</span> sockaddr *) &amp;cliAddr,&amp;cliLen);
<a name="l00082"></a>00082       <span class="keywordflow">if</span>(n&lt;n) {
<a name="l00083"></a>00083         printf(<span class="stringliteral">"%s : cannot receive data\n"</span>,argv[0]);
<a name="l00084"></a>00084         <span class="keywordflow">continue</span>;
<a name="l00085"></a>00085       }
<a name="l00086"></a>00086 
<a name="l00087"></a>00087       printf(<span class="stringliteral">"%s : from %s:%d on %s : %s\n"</span>,argv[0],
<a name="l00088"></a>00088              inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),
<a name="l00089"></a>00089              argv[1],
<a name="l00090"></a>00090              msg);
<a name="l00091"></a>00091     }<span class="comment">/* end of infinite server loop */</span>
<a name="l00092"></a>00092 
<a name="l00093"></a>00093   }
<a name="l00094"></a>00094 
<a name="l00095"></a>00095 <span class="keywordflow">return</span> 0;
<a name="l00096"></a>00096 
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098   
<a name="l00099"></a>00099 
<a name="l00100"></a>00100   
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:25 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
